package com.huawei.hwid.core.b.a.a;

import android.content.Context;
import android.os.Bundle;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.p;
import com.huawei.hwid.core.datatype.ChildrenInfo;
import com.huawei.hwid.core.datatype.DeviceInfo;
import com.huawei.hwid.core.datatype.TmemberRight;
import com.huawei.hwid.core.datatype.UserAccountInfo;
import com.huawei.hwid.core.datatype.UserInfo;
import com.huawei.hwid.core.datatype.UserLoginInfo;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.f.g;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class b extends com.huawei.hwid.core.b.a.a {
    private Context A;
    private String h;
    private String i;
    private UserInfo j;
    private UserLoginInfo k;
    private ArrayList<DeviceInfo> l;
    private ArrayList<UserAccountInfo> m;
    private ArrayList<TmemberRight> n;
    private ArrayList<ChildrenInfo> o;
    private String p = "/IUserInfoMng/getUserInfo";
    private boolean q;
    private boolean r;
    private boolean s;
    private boolean t;
    private boolean u;
    private boolean v;
    private DeviceInfo w;
    private UserAccountInfo x;
    private TmemberRight y;
    private ChildrenInfo z;

    static class a extends com.huawei.hwid.core.helper.handler.a {
        private CloudRequestHandler a;

        a(Context context, CloudRequestHandler cloudRequestHandler) {
            super(context);
            this.a = cloudRequestHandler;
        }

        public void a(Bundle bundle) {
            super.a(bundle);
            com.huawei.hwid.core.d.b.f("getUserInfo");
            UserInfo userInfo = (UserInfo) bundle.getParcelable("userInfo");
            UserLoginInfo userLoginInfo = (UserLoginInfo) bundle.getParcelable("userLoginInfo");
            ArrayList parcelableArrayList = bundle.getParcelableArrayList("devicesInfo");
            ArrayList parcelableArrayList2 = bundle.getParcelableArrayList("accountsInfo");
            ArrayList parcelableArrayList3 = bundle.getParcelableArrayList(HwAccountConstants.EXTRA_MEMBERRIGHTS);
            Bundle bundle2 = new Bundle();
            bundle2.putParcelableArrayList("userAccountInfo", parcelableArrayList2);
            bundle2.putParcelableArrayList("deviceInfo", parcelableArrayList);
            bundle2.putParcelable("userInfo", userInfo);
            bundle2.putParcelable("userLoginInfo", userLoginInfo);
            bundle2.putParcelableArrayList(HwAccountConstants.EXTRA_MEMBERRIGHTS, parcelableArrayList3);
            this.a.onFinish(bundle2);
        }

        public void b(Bundle bundle) {
            super.b(bundle);
            com.huawei.hwid.core.d.b.f("getUserInfo");
            ErrorStatus errorStatus = (ErrorStatus) bundle.getParcelable("requestError");
            if (errorStatus != null) {
                this.a.onError(errorStatus);
            } else {
                this.a.onError(new ErrorStatus(32, "ErrorStatus is null"));
            }
        }
    }

    public b(Context context, String str, String str2, Bundle bundle) {
        this.A = context;
        f(str);
        g(str2);
        a(true);
    }

    public String j() throws IllegalArgumentException, IllegalStateException, IOException {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            XmlSerializer a = p.a(byteArrayOutputStream);
            a.startDocument("UTF-8", Boolean.valueOf(true));
            a.startTag(null, "GetUserInfoReq");
            p.a(a, "version", "27120");
            p.a(a, "userID", this.h);
            p.a(a, "queryRangeFlag", this.i);
            p.a(a, UserInfo.LANGUAGECODE, c(this.A));
            a.endTag(null, "GetUserInfoReq");
            a.endDocument();
            String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8");
            e.b("GetUserInfoRequest", "packedString", true);
            return byteArrayOutputStream2;
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.b("GetUserInfoRequest", "IOException", true);
            }
        }
    }

    private void a(String str, XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
        if ("userID".equals(str) && !this.u) {
            this.h = xmlPullParser.nextText();
        } else if ("userInfo".equals(str)) {
            this.j = new UserInfo();
            this.q = true;
        } else if (this.q) {
            UserInfo.getUserInfoIntag(xmlPullParser, this.j, str);
        } else if ("userLoginInfo".equals(str)) {
            this.k = new UserLoginInfo();
            this.r = true;
        } else if (this.r) {
            UserLoginInfo.getUserLoginInfoInTag(xmlPullParser, this.k, str);
        } else if (DeviceInfo.TAG_DEVICE_INFO_LIST.equals(str)) {
            this.l = new ArrayList();
            this.s = true;
        } else if ("DeviceInfo".equals(str)) {
            this.w = new DeviceInfo();
        } else if (this.s) {
            DeviceInfo.getDeviceInfoInTag(xmlPullParser, this.w, str);
        } else if (UserAccountInfo.TAG_USERACCTINFO_LIST.equals(str)) {
            this.m = new ArrayList();
            this.t = true;
        } else {
            b(str, xmlPullParser);
        }
    }

    private void b(String str, XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
        if (UserAccountInfo.TAG_USERACCINFO.equals(str)) {
            this.x = new UserAccountInfo();
        } else if (this.t) {
            UserAccountInfo.getUserAccInfoInTag(xmlPullParser, this.x, str);
        } else if (UserAccountInfo.TAG_MEMBERRIGHTLIST.equals(str)) {
            this.n = new ArrayList();
            this.u = true;
        } else if (UserAccountInfo.TAG_MEMBERRIGHT.equals(str)) {
            this.y = new TmemberRight();
        } else if (this.u) {
            TmemberRight.a(xmlPullParser, this.y, str);
        } else if (ChildrenInfo.TAG_CHILDREN_INFO_LIST.equals(str)) {
            this.o = new ArrayList();
            this.v = true;
        } else {
            ChildrenInfo childrenInfo = this.z;
            if (ChildrenInfo.TAG_CHILDREN_INFO.equals(str)) {
                this.z = new ChildrenInfo();
            } else if (this.v) {
                ChildrenInfo.a(xmlPullParser, this.z, str);
            }
        }
    }

    private void c(String str, XmlPullParser xmlPullParser) throws XmlPullParserException, IOException {
        if ("userInfo".equals(str)) {
            this.q = false;
        } else if ("userLoginInfo".equals(str)) {
            this.r = false;
        } else if (DeviceInfo.TAG_DEVICE_INFO_LIST.equals(str)) {
            this.s = false;
        } else if ("DeviceInfo".equals(str)) {
            this.l.add(this.w);
        } else if (UserAccountInfo.TAG_USERACCINFO.equals(str)) {
            this.m.add(this.x);
        } else if (UserAccountInfo.TAG_USERACCTINFO_LIST.equals(str)) {
            this.t = false;
        } else if (UserAccountInfo.TAG_MEMBERRIGHT.equals(str)) {
            this.n.add(this.y);
        } else if (UserAccountInfo.TAG_MEMBERRIGHTLIST.equals(str)) {
            this.u = false;
        } else if (ChildrenInfo.TAG_CHILDREN_INFO.equals(str)) {
            this.o.add(this.z);
        }
    }

    public void a(String str) throws XmlPullParserException, IOException {
        this.q = false;
        this.r = false;
        this.s = false;
        this.t = false;
        this.u = false;
        this.v = false;
        this.w = null;
        this.x = null;
        this.y = null;
        this.z = null;
        XmlPullParser a = p.a(str.getBytes("UTF-8"));
        for (int eventType = a.getEventType(); 1 != eventType; eventType = a.next()) {
            String name = a.getName();
            switch (eventType) {
                case 2:
                    if (HiAnalyticsConstant.BI_KEY_RESUST.equals(name)) {
                        this.b = g.a(a.getAttributeValue(null, "resultCode"));
                    }
                    if (this.b != 0) {
                        if (!"errorCode".equals(name)) {
                            if (!"errorDesc".equals(name)) {
                                break;
                            }
                            this.d = a.nextText();
                            break;
                        }
                        this.c = g.a(a.nextText());
                        break;
                    }
                    a(name, a);
                    break;
                case 3:
                    c(name, a);
                    break;
                default:
                    break;
            }
        }
    }

    public String b() {
        return this.p;
    }

    private void f(String str) {
        this.h = str;
    }

    private void g(String str) {
        this.i = str;
    }

    private UserInfo s() {
        return this.j;
    }

    private UserLoginInfo t() {
        return this.k;
    }

    private ArrayList<DeviceInfo> u() {
        return this.l;
    }

    private ArrayList<UserAccountInfo> v() {
        return this.m;
    }

    private ArrayList<TmemberRight> w() {
        return this.n;
    }

    private ArrayList<ChildrenInfo> x() {
        return this.o;
    }

    public Bundle c() {
        Bundle c = super.c();
        c.putParcelableArrayList("accountsInfo", v());
        c.putParcelableArrayList("devicesInfo", u());
        c.putParcelableArrayList(HwAccountConstants.EXTRA_MEMBERRIGHTS, w());
        c.putParcelableArrayList("childrenInfo", x());
        c.putParcelable("userInfo", s());
        c.putParcelable("userLoginInfo", t());
        return c;
    }

    public void a(Context context, com.huawei.hwid.core.b.a.a aVar, String str, CloudRequestHandler cloudRequestHandler) {
        com.huawei.hwid.core.b.a.e.a(context, aVar, str, a(context, aVar, new a(context, cloudRequestHandler)));
    }

    public String c(Context context) {
        return com.huawei.hwid.core.d.b.f(context);
    }
}
